package com.power.services.project;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.project.RzProjectDAO;
import com.power.model.funding.RzMoneyside;
import com.power.model.project.RzProject;
import com.power.util.PageUtil;

@Service("rzProjectService")
public class RzProjectServiceImpl implements RzProjectService {

	@Autowired
	private RzProjectDAO dao;

	public void create(RzProject arg0) {
		dao.save(arg0);
	}

	public void delete(RzProject arg0) {
		dao.delete(arg0);
	}

	public RzProject read(Integer arg0) {
		return dao.get(arg0);
	}

	public void update(RzProject arg0) {
		dao.update(arg0);
	}

	public List<RzProject> findALLbyParam(Map<String, String> map, int start,
			int limit) {
		String hql = "from com.power.model.project.RzProject";
		return dao.find(hql);
	}

	public void saveOrUpdate(RzProject obj) {
		dao.saveOrUpdate(obj);		
	}

	public PageUtil showProject(String keyword, int pageNo, int limit) {
		PageUtil pu = new PageUtil();
		DetachedCriteria criteria = DetachedCriteria.forClass(RzProject.class);
		if(!"".equals(keyword)){
		criteria.add(Restrictions.eq("financingType", keyword));
		}
		criteria.addOrder(Order.asc("updateTime"));
		List<RzProject> list = dao.findByCriteria(criteria, (pageNo-1)*limit, limit);
		pu.setList(list);
		pu.setPageNo(pageNo);
		pu.setPageSize(limit);
		pu.setTotalRecords(dao.getRowCount(criteria));
		return pu;
	}
	
	@SuppressWarnings("unchecked")
	public List<RzProject> findRecommend() {
		String hql = "select d from com.power.model.project.RzProject d  where d.recommend='0' order by id desc ";
		return dao.find(hql,0,2);
	}
	
	@SuppressWarnings("unchecked")
	public List<RzProject> findHot() {
		String hql = "select d from com.power.model.project.RzProject d  where d.isHot='0' order by id desc ";
		return dao.find(hql,0,10);
	}
	
	@SuppressWarnings("unchecked")
	public List<RzProject> findlist(int limit,String str) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RzProject.class);
		if(str!=null){
		criteria.add(Restrictions.eq("financingType", str));
		}
		criteria.addOrder(Order.asc("updateTime"));
		List<RzProject> list = dao.findByCriteria(criteria, 0, limit);
		return list;
	}

}
